package com.itheima.book.controller;

import com.itheima.book.pojo.dto.BooksPageQueryDTO;
import com.itheima.book.pojo.entity.Book;
import com.itheima.book.service.BookService;
import com.itheima.book.utils.PageHelper;
import com.itheima.book.utils.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/api")
@Api(tags = "书籍市场相关接口")
public class BookMarketController {
    @Autowired
    private BookService bookService;

    /**
     * 分页查询书籍市场列表
     * @param currentPage 当前页码
     * @param pageSize 每页显示记录数
     * @param searchQuery 搜索关键词
     * @param sortBy 排序方式
     * @return
     */
    @GetMapping("/books/list")
    @ApiOperation("分页查询书籍市场列表")
    public Result<PageHelper<Book>> getMarketBooks(
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "8") int pageSize,
            @RequestParam(required = false) String searchQuery,
            @RequestParam(required = false, defaultValue = "newest") String sortBy) {
        log.info("分页查询书籍市场列表: currentPage={}, pageSize={}, searchQuery={}, sortBy={}", 
                currentPage, pageSize, searchQuery, sortBy);
        
        // 构建查询DTO
        BooksPageQueryDTO queryDTO = new BooksPageQueryDTO();
        queryDTO.setCurrentPage(currentPage);
        queryDTO.setPageSize(pageSize);
        queryDTO.setSearchQuery(searchQuery);
        queryDTO.setSortBy(sortBy);
        
        // 调用service层方法
        PageHelper<Book> pageHelper = bookService.getmarket(queryDTO);
        return Result.success(pageHelper);
    }
} 